/**************************************************************
 *@ProjectName: blweibo
 *@FileName: LookAroundListActivity.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.zhiweibo.models.Topic;
import com.zhiweibo.util.Constant;
import com.zhiweibo.util.HttpUtil;

/**
 * The Class LookAroundListActivity.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class LookAroundListActivity extends BaseActivity implements OnItemClickListener {

    /** The m list. */
    private List<Topic> mList = new ArrayList();

    /** The adapter. */
    private BaseAdapter adapter;

    /** The mlv. */
    private ListView mlv;

    /** The count. */
    private int count = 50;

    /**
     * Load more.
     */
    private void loadMore() {
        reloadBegine(0);
        count += 50;
        if (count > 200) {
            count = 200;
        }
        new LookTask().execute(0);
    }

    /**
     * Reload.
     */
    private void reload() {
        reloadBegine(1);
        count = 50;
        new LookTask().execute(0);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#handleTitleBarEvent(int)
     */
    @Override
    protected void handleTitleBarEvent(int paramInt) {
        switch (paramInt) {
        default:
        case 0:
            setResult(-1, null);
            finish();
            break;
        case 1:
            finish();
            break;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.listbase);
        setTitleBar(1, getString(R.string.imageviewer_back), getString(R.string.link_lookaround), null);
        mlv = (ListView) findViewById(R.id.lvUser);
        mlv.setBackgroundColor(-1);
        adapter = new WeiboListAdapter();
        mlv.setAdapter(adapter);
        mlv.setOnItemClickListener(this);
        reload();
    }

    /**
     * The Class WeiboListAdapter.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class WeiboListAdapter extends BaseAdapter {

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            if (mList == null || mList.size() == 0) {
                return 1;
            } else {
                return mList.size() + 2;
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int paramInt) {
            return paramInt;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int paramInt) {
            return paramInt;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getView(int, android.view.View,
         * android.view.ViewGroup)
         */
        @Override
        public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
            if (mList == null || mList.size() == 0) {
                return mReloadItem;
            } else {
                paramInt--;
                if (paramInt == -1) {
                    return mReloadItem;
                } else if (paramInt == mList.size()) {
                    return mLoadmoreItem;
                } else {
                    try {
                        if (paramView != null && paramView instanceof MBlogListItemView) {
                            ((MBlogListItemView) paramView).update(LookAroundListActivity.this, mList.get(paramInt));
                            return paramView;
                        } else {
                            return new MBlogListItemView(LookAroundListActivity.this, mList.get(paramInt));
                        }
                    } catch (Exception e) {
                        return null;
                    }
                }
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.BaseAdapter#getViewTypeCount()
         */
        @Override
        public int getViewTypeCount() {
            if (mList == null) {
                return 1;
            } else {
                return 4;
            }
        }

        /**
         * Refresh.
         */
        public void refresh() {
            notifyDataSetChanged();
        }
    }

    /**
     * The Class LookTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class LookTask extends AsyncTask {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... paramArrayOfString) {
            mList = HttpUtil.getTopics(LookAroundListActivity.this, Constant.PUBLIC_TIMELINE + "?count=" + count);
            return mList;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            adapter.notifyDataSetChanged();
            reloadEnd();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
     * .AdapterView, android.view.View, int, long)
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if (view == mReloadItem) {
            reload();
        } else if (view == mLoadmoreItem) {
            loadMore();
        } else {
            position--;
            // startActivity(new Intent(this,
            // DetailWeiboActivity.class).putExtra("topic",
            // mList.get(position)));
        }
    }
}
